package cn.ydt.ydt.community.webapi.service.impl;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.common.utils.FileNameUtils;
import cn.ydt.ydt.community.service.ICommunityService;
import cn.ydt.ydt.community.webapi.mapper.CommunityMapper;
import cn.ydt.ydt.community.webapi.utils.SecurityContextUtils;
import cn.ydt.ydt.pojo.community.model.Community;
import cn.ydt.ydt.pojo.community.vo.SimpleCommunityVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommunityServiceImpl implements ICommunityService {
    @Autowired
    CommunityMapper communityMapper;

    @Override
    public SimpleCommunityVO getSimCommunityById() {

        Long communityId = SecurityContextUtils.getNet().getCommunityId();
        Community community = communityMapper.getCommunityById(communityId);
        if (community==null){
            throw new YdtServiceException(StatusCode.BAD_REQUEST,"该社区不存在！");
        }
        SimpleCommunityVO simpleCommunityVO = new SimpleCommunityVO();
        BeanUtils.copyProperties(community,simpleCommunityVO);
        String communityIcon = simpleCommunityVO.getCommunityIcon();
        simpleCommunityVO.setCommunityIcon(FileNameUtils.filePath(communityIcon));
        return simpleCommunityVO;
    }
}
